package com.chinackts.auth.repository.redis;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author Lianhong
 * @since 2021/11/1
 */
@Repository
public class SocialCreditCodeRepository {

    @Resource
    private StringRedisTemplate redisTemplate1;

    private static final String KEY = "%s:socialCreditCode";

    @Value("${e-port.redis.timeout}")
    private long timeout;


    public String key(String id) {
        return String.format(KEY, id);
    }

    /**
     * 获取企业税号
     *
     * @param id 用户标识
     */
    public String getSocialCreditCode(String id) {
        ValueOperations<String, String> ops = this.redisTemplate1.opsForValue();
        String socialCreditCode = ops.get(String.format(KEY, id));
        if (StringUtils.isEmpty(socialCreditCode)) return null;
        return socialCreditCode;
    }

    public void saveSocialCreditCode(String id, String socialCreditCode) {
        ValueOperations<String, String> ops = this.redisTemplate1.opsForValue();
        ops.set(String.format(KEY, id), socialCreditCode, timeout, TimeUnit.HOURS);
    }
}
